package net.da.client.common.ui.widget;

import net.da.client.common.component.AbstractComponent;
import net.da.client.common.component.ComponentDispatcher;
import net.da.client.common.component.ComponentDispatcherImpl;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class NestNavigationPanel extends Composite {

	// private

	public NestNavigationPanel(final AbstractComponent component, String[] compNames) {
		
		if (compNames == null) {
			compNames = new String[]{};
		}

		DockPanel dockPanel = new DockPanel();
		HorizontalPanel hp0 = new HorizontalPanel();
		HorizontalPanel hp1 = new HorizontalPanel();
		BreadCrumb breadCrumb = new BreadCrumb();
		
		dockPanel.add(breadCrumb, DockPanel.NORTH);
		dockPanel.add(hp0, DockPanel.NORTH);
		dockPanel.add(hp1, DockPanel.SOUTH);
		initWidget(dockPanel);
		
		final ComponentDispatcher dispatcher = ComponentDispatcherImpl.getInstance();		
		String[] ids = dispatcher.getNestedComponentNames();
		int idsCount = ids.length;
		for (int i = 0; i < idsCount; i++) {
			String id = ids[i];
			final int offset = idsCount - i;
			breadCrumb.addActiveButtonLink(id, new ClickListener(){

				public void onClick(Widget sender) {
					ComponentDispatcher dispatcher = ComponentDispatcherImpl.getInstance();
					dispatcher.returnToPriorComponent(offset);
				}	
			});			
		}
		breadCrumb.addInactiveButtonLink(component.getComponentDef().getName());
				

		for (int i = 0; i < compNames.length; i++) {
			final String name = compNames[i];
			Button button = new Button(name);
			hp0.add(button);
			if (name.equals(component.getComponentDef().getName())) {
				//button.setEnabled(false);
			}

			button.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {					
					dispatcher.nestTo(name);
				}
			});
		}

		Button rtButton = new Button("Return");
		hp1.add(rtButton);

		rtButton.setEnabled(dispatcher.isReturnToPriorComponentPossible());
		rtButton.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {				
				dispatcher.returnToPriorComponent();
			}
		});

	}

}
